package com.kabigon.tiny.task;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.NumberUtil;
import com.kabigon.tiny.dto.MediationDto;
import com.kabigon.tiny.util.LocalJsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定时任务
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.tiny.task
 * @data 2022/11/2 22:02
 */
@Slf4j
@Component
public class ScheduledTask {

    @Scheduled(cron = "0/10 * * * * ?")
    public void mediationTask() {
        log.info("开始");
        List<MediationDto> list = LocalJsonUtil.getListFormJson("json/mediation.json", MediationDto.class);
        // 分片处理 4
        List<List<MediationDto>> partition = ListUtil.partition(list, 4);
        for (List<MediationDto> dtos : partition) {
            //int i = RandomUtil.randomInt();
            try {
                if (NumberUtil.isEven(2)) {
                    throw new RuntimeException();
                } else {
                    log.info("dtos size: {}", dtos.size());
                }
            } catch (RuntimeException e) {
                log.error("mediationTask 发生异常：{}", ExceptionUtil.getSimpleMessage(e));
            }
        }
        //log.info("json : {}", JSONUtil.toJsonPrettyStr(list));
    }

    public static void main(String[] args) {
        log.info("time : {}", DateUtil.formatDateTime(new Date(1604326477000L)));
    }

}
